<template>
  <el-dialog :title="dialogTitle" :visible.sync="nllmDialogVisible" width="60%">
    <el-form :model="nllmForm" :rules="rules" ref="nllmForm" label-width="100px">
      <el-form-item v-show="false" label="类目编号" prop="jkflbh">
        <el-input v-model="nllmForm.jkflbh"></el-input>
      </el-form-item>
      <el-form-item label="类目名称" prop="jkflmc">
        <el-input v-model="nllmForm.jkflmc" maxlength="30" show-word-limit></el-input>
      </el-form-item>
      <el-form-item v-show="false" label="序号" prop="xh">
        <el-input v-model="nllmForm.xh"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="nllmDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addOrUpdateJkfl">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import jkglService from '@/api/jkgl'

export default {
  data() {
    return {
      //能力类目弹框
      nllmDialogVisible: false,
      nllmForm: {
        jkflbh: "",
        jkflmc: "",
        xh: "",
      },
      rules: {
        jkflmc: [
          { required: true, message: '请输入类目名称', trigger: 'blur' }
        ],
      },
      dialogTitle: '',

    }
  },
  mounted() {
    //
  },
  methods: {
    /*
     * 打开弹框
     */
    openDialog(val) {
      this.nllmDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.nllmForm.resetFields();
        if (val === '') {
          this.dialogTitle = '新增'
        } else {
          this.dialogTitle = '修改';
          const params = { jkflbh: val }
          jkglService.getJkfl(params).then(res => {
            this.nllmForm.jkflbh = res.jkflbh;
            this.nllmForm.xh = res.xh;
            this.nllmForm.jkflmc = res.jkflmc;
          })
            .catch(err => {
              !err.isError &&
                this.$message({ type: 'warning', message: err.message })
            })
        }
      });
    },
    /*
     * 新增能力分类
     */
    addNlfl() {
      const loading = this.$loading({
        lock: true,
        text: '新增能力中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      const params = this.nllmForm;
      jkglService
        .addJkfl(params)
        .then(res => {
          loading.close();
          this.nllmDialogVisible = false;
          this.$message({ message: '新增能力成功！', type: 'success' });
          //刷新能力表格
          this.$emit('updateTree');
        })
        .catch(err => {
          loading.close();
          !err.isError &&
            this.$message({ type: 'warning', message: err.message })
        })
    },
    /*
     * 修改能力分类
     */
    editorNlfl() {
      const loading = this.$loading({
        lock: true,
        text: '修改能力中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      const params = this.nllmForm;
      jkglService
        .updateJkfl(params)
        .then(res => {
          loading.close();
          this.nllmDialogVisible = false;
          this.$message({ message: '修改能力成功！', type: 'success' });
          //刷新能力
          this.$emit('updateTree');
        })
        .catch(err => {
          loading.close();
          !err.isError &&
            this.$message({ type: 'warning', message: err.message })
        })
    },
    /** 新增活更新能力分类 */
    addOrUpdateJkfl() {
      this.$refs.nllmForm.validate((valid) => {
        if (valid) {
          if (this.nllmForm.jkflbh === '') {
            //新增
            this.addNlfl();
          } else {
            //更新
            this.editorNlfl();
          }
        } else {
          return false;
        }
      });
    },

  }
}
</script>
<style lang="scss" scoped>
</style>
